var express = require('express');
var router = express.Router();
const { queryByWoId, updateColorByWoId } = require('../dataBase/api');

/* GET users listing. */
router.get('/selectColor/:woId', async function (req, res, next) {
  try {
    const woId = req.params.woId;
    
    // 参数验证
    if (!woId) {
      return res.status(400).json({ 
        success: false, 
        message: '工单ID不能为空' 
      });
    }

    const sql = `
        SELECT COLOUR, LOT_ID, CREATED, PARENT_LOT_RRN, EFF 
        FROM wip_lot 
        WHERE WO_ID = @woId 
        AND COLOUR <> 'DA' 
        AND LOT_ID LIKE '%86%' 
        AND PARENT_LOT_RRN IS NULL 
        ORDER BY CREATED
    `;

    const result = await queryByWoId(sql, woId, 'MES_DB');
    res.json(result);
  } catch (error) {
    next(error);
  }
});

/* PUT update color */
router.put('/updateColor/:woId', async function (req, res, next) {
  try {
    const woId = req.params.woId;
    
    // 参数验证
    if (!woId) {
      return res.status(400).json({ 
        success: false, 
        message: '工单ID不能为空' 
      });
    }

    const result = await updateColorByWoId(woId, 'MES_DB');
    res.json(result);
  } catch (error) {
    next(error);
  }
});

module.exports = router;
